package com.healthcloud.controller.modules.instrument;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.healthcloud.annotation.SysLog;
import com.healthcloud.entity.MsgVo;
import com.healthcloud.entity.SysUserEntity;
import com.healthcloud.entity.TestresultEntity;
import com.healthcloud.service.SysUserService;
import com.healthcloud.service.TestresultService;
import com.healthcloud.utils.ApiRes;
import com.healthcloud.utils.PageParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.healthcloud.entity.RestCompanyEntity;
import com.healthcloud.service.RestCompanyService;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 
 * 
 * @author yucl
 * @email yucailong1111@126.com
 * @date 2018-05-30 10:59:37
 */
@ApiIgnore
@Controller
@RequestMapping("restcompany")
public class RestCompanyController {
	@Autowired
	private RestCompanyService restCompanyService;

	@Autowired
    private SysUserService sysUserService;

	@Autowired
    private TestresultService testresultService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public String list(@RequestParam Map<String, Object> params){
		return "newframe/restCompany/companymgr";
	}

	@RequestMapping(value = "/searchRestCompany")
	@ResponseBody
    public MsgVo  searchRestCompany(RestCompanyEntity bean,Model model, @ModelAttribute("pageParam")PageParam pageParam){
        List<RestCompanyEntity> list = restCompanyService.queryList(bean, pageParam);
        int count = restCompanyService.queryTotal(bean);
        return new MsgVo(list,count,count,pageParam.getsEcho());
    }
	
	/**
	 * 新增页
	 */
	@RequestMapping("/tosave")
    public String toSave(Model model) {
        return "newframe/restCompany/addOrUpdaterestcompany";
    }
    
	/**
	 * 保存
	 */
    @SysLog("保存RestCompany")
    @RequestMapping("/save")
    @ResponseBody
    public ApiRes save(RestCompanyEntity entity) {
        entity.setCreateTime(new Date());
        restCompanyService.save(entity);
        return ApiRes.SUCCESS;
    }
	
	/**
	 * 修改页
	 */	
	@RequestMapping("/toupdate")
    public String toUpdate(@Param("id") Integer id, Model model) {

        RestCompanyEntity entity = restCompanyService.queryObject(id);
        model.addAttribute("entity", entity);

        SysUserEntity sysUserEntity = sysUserService.queryObjectByid(entity.getUserId());
        model.addAttribute("sysuser",sysUserEntity);
        return "newframe/restCompany/addOrUpdaterestcompany";
    }
	
    /**
     * 修改
     */
    @SysLog("修改RestCompany")
    @RequestMapping("/update")
    @ResponseBody
    public ApiRes update(RestCompanyEntity entity) {
        entity.setUpdateTime(new Date());
        restCompanyService.update(entity);
        return ApiRes.SUCCESS;
    }

	
    /**
     * 删除
     */
    @SysLog("删除RestCompany")
    @RequestMapping("/delete")
    @ResponseBody
    public ApiRes delete(Integer[] ids) {
        restCompanyService.deleteBatch(ids);
        return ApiRes.SUCCESS;
    }

    /**
     * 跳转至详细数据页
     */
    @RequestMapping(value = "/show")
    public String show(@Param("id") String id, Model model){
//        RestCompanyEntity entity = restCompanyService.queryByName(companyname);
        model.addAttribute("companyId",id);
        return "newframe/restCompany/testresultmgr";
    }

    /**
     * 查询检测数据
     */
    @RequestMapping(value = "/searchTestresult")
    @ResponseBody
    public MsgVo  searchTestresult(TestresultEntity bean, @ModelAttribute("pageParam")PageParam pageParam, String companyId){

        List<TestresultEntity> list = testresultService.queryResult(bean, companyId, pageParam);
        if(list != null) {
            //0:未发送  1：发送成功  2：发送失败  3：二次发送失败
            for(TestresultEntity entity : list) {
                if(entity.getIsPublish() == 0) {
                    entity.setOpereation("未发送");
                }else if(entity.getIsPublish() == 1) {
                    entity.setOpereation("发送成功");
                }else if(entity.getIsPublish() == 2) {
                    entity.setOpereation("发送失败");
                }else if(entity.getIsPublish() == 3) {
                    entity.setOpereation("二次发送失败");
                }
            }
        }
        int count = testresultService.countResult(bean, companyId);
        return new MsgVo(list,count,count,pageParam.getsEcho());
    }
}
